import React from 'react'
import { Form, Input, Button } from 'antd';
import './index.scss'
import img_one from '../../img/圈钱天团.jpeg'
import { apiLogin } from '../../api'
import Cookie from 'js-cookie'
import { useNavigate } from 'react-router-dom'

const Login = () => {
    const navigate = useNavigate()
    const onFinish = (values: any) => {
        apiLogin(values).then((res: any) => {
            Cookie.set('token', res.token)
            window.location.href = "/"
        })
    };

    const onFinishFailed = (errorInfo: any) => {
        console.log('Failed:', errorInfo);
    };

    const handleSigIn = () => {
        navigate('/sigin')
    }
    return (
        <div className='login'>
            <div className='loginTab'>
                <div className='loginTabLeft'>
                    <img src={img_one} alt="" />
                </div>
                <div className='loginTabRight'>
                    <Form
                        name="basic"
                        labelCol={{ span: 8 }}
                        wrapperCol={{ span: 16 }}
                        initialValues={{ remember: true }}
                        onFinish={onFinish}
                        onFinishFailed={onFinishFailed}
                        autoComplete="off"
                    >
                        <Form.Item
                            label="账号"
                            name="username"
                            rules={[{ required: true, message: 'Please input your username!' }]}
                        >
                            <Input />
                        </Form.Item>

                        <Form.Item
                            label="密码"
                            name="password"
                            rules={[{ required: true, message: 'Please input your password!' }]}
                        >
                            <Input.Password />
                        </Form.Item>

                        <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
                            <Button type="primary" htmlType="submit">
                                登录
                            </Button>
                            <Button onClick={() => handleSigIn()}>注册</Button>
                        </Form.Item>
                    </Form>
                </div>

            </div>
        </div>
    )
}

export default Login
